//
//  UIBarButtonItem+Extension.m
//  BasicFramework
//
//  Created by Rainy on 16/10/26.
//  Copyright © 2016年 Rainy. All rights reserved.
//

#import "UIBarButtonItem+Extension.h"
#import "UIImage+Image.h"

#import "UIButton+layout.h"
#import "NSString+Tools.h"
@implementation UIBarButtonItem (Item)


//标题
+(UIBarButtonItem *)OnCreateBarBtnItemName:(NSString *)ItemName BarBtnstyle:(BarBtnItemStyle)ItemStyle Btntag:(int)tag target:(id)target action:(SEL)action
{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:ItemName forState:UIControlStateNormal];
    if (ItemStyle==LeftBarBtnItemStyle) {
        button.titleLabel.textAlignment = NSTextAlignmentLeft;
    }else
    {
        button.titleLabel.textAlignment = NSTextAlignmentRight;
    }
    button.titleLabel.font = kSysFont(kFitW(15));
    //button.frame = CGRectMake(0, 0, 45, 45);
    
    // 设置按钮的尺寸为背景图片的尺寸+文字大小
    //    button.width =[ItemName sizeWithFont:button.titleLabel.font maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)].width;
    //
    //    button.height = button.currentImage.size.height+[ItemName sizeWithFont:button.titleLabel.font maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)].height;
    //
    //    [button setContentVerticalAlignment:UIControlContentVerticalAlignmentTop];
    //    //设置title在button上的位置（上top，左left，下bottom，右right）
    //    button.titleEdgeInsets = UIEdgeInsetsMake(30,-30, 0, 0);
    
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.tag = tag;
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [button sizeToFit];
    UIBarButtonItem *BarItem = [[self alloc]initWithCustomView:button];
    //BarItem.customView = button;
    return BarItem;
}


//图片
+(UIBarButtonItem *)OnCreateBarBtnItemImage:(NSString *)BtnItemImage BarBtnstyle:(BarBtnItemStyle)ItemStyle Btntag:(int)tag target:(id)target action:(SEL)action
{
//    UIImage *imageName = [UIImage imageNamed:BtnItemImage];
//    UIBarButtonItem *BarItem = [[UIBarButtonItem alloc]initWithImage:imageName style:(UIBarButtonItemStylePlain) target:target action:action];
//
//    if (ItemStyle==LeftBarBtnItemStyle) {
//        BarItem.customView.contentMode =UIViewContentModeLeft;
//
//    }else
//    {
//        BarItem.customView.contentMode =UIViewContentModeRight;
//    }
//    BarItem.tintColor = [UIColor whiteColor];

    
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    if (ItemStyle==LeftBarBtnItemStyle) {
        button.imageView.contentMode =UIViewContentModeLeft;
    }else
    {
        button.imageView.contentMode =UIViewContentModeRight;
    }
    UIImage *image = [[UIImage imageNamed:BtnItemImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //    [button setBackgroundImage:[UIImage imageNamed:BtnItemImage] forState:UIControlStateNormal];
    [button setImage:image forState:UIControlStateNormal];
    //button.frame = CGRectMake(0, 0, 45, 45);
    button.tag = tag;
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    [button sizeToFit];
    
    
    UIBarButtonItem *BarItem = [[UIBarButtonItem alloc]initWithCustomView:button];

    return BarItem;
}

+(UIBarButtonItem *)OnCreateBarBtnItemImage:(NSString *)BtnItemImage ItemName:(NSString *)ItemName BarBtnstyle:(BarBtnItemStyle)ItemStyle Btntag:(int)tag target:(id)target action:(SEL)action
{
    UIBarButtonItem *BarItem = [[UIBarButtonItem alloc]init];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:BtnItemImage] forState:UIControlStateNormal];
    [button setTitle:ItemName forState:UIControlStateNormal];
    //button.frame = CGRectMake(0, 0, 45, 45);
    [button sizeToFit];
    if (ItemStyle==LeftBarBtnItemStyle) {
        button.imageView.contentMode =UIViewContentModeLeft;
        [button layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:13];
    }else
    {
        button.imageView.contentMode =UIViewContentModeRight;
        [button layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:8];
    }
    button.tag = tag;
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    BarItem.customView = button;
    return BarItem;
}


+ (UIBarButtonItem *)OnCreateBarBtnWithImage:(NSString*)BtnItemImage highImage:(NSString *)HighImage  BarBtnstyle:(BarBtnItemStyle)ItemStyle target:(id)target action:(SEL)action
{
    // btn
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    if (ItemStyle==LeftBarBtnItemStyle) {
        btn.imageView.contentMode =UIViewContentModeLeft;
    }else
    {
        btn.imageView.contentMode =UIViewContentModeRight;
    }
    [btn setBackgroundImage:[UIImage imageWithOriginalName:BtnItemImage] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageWithOriginalName:HighImage] forState:UIControlStateHighlighted];
    
    btn.size = btn.currentBackgroundImage.size;
    
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    return  [[self alloc] initWithCustomView:btn];
}
@end



@implementation UIBarButtonItem (Hentai)

#pragma mark - private instance method

- (void)invokeBlockAction {
    BlockAction blockAction = [self blockAction];
    if (blockAction) {
        blockAction();
    }
}

#pragma mark - instance method

- (instancetype)initWithImage:(UIImage *)image style:(UIBarButtonItemStyle)style blockAction:(BlockAction)blockAction {
    [self setBlockAction:blockAction];
    self = [self initWithImage:image style:style target:self action:@selector(invokeBlockAction)];
    return self;
}

- (instancetype)initWithImage:(UIImage *)image landscapeImagePhone:(UIImage *)landscapeImagePhone style:(UIBarButtonItemStyle)style blockAction:(BlockAction)blockAction {
    [self setBlockAction:blockAction];
    self = [self initWithImage:image landscapeImagePhone:landscapeImagePhone style:style target:self action:@selector(invokeBlockAction)];
    return self;
}

- (instancetype)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style blockAction:(BlockAction)blockAction {
    [self setBlockAction:blockAction];
    self = [self initWithTitle:title style:style target:self action:@selector(invokeBlockAction)];
    return self;
}

- (instancetype)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem blockAction:(BlockAction)blockAction {
    [self setBlockAction:blockAction];
    self = [self initWithBarButtonSystemItem:systemItem target:self action:@selector(invokeBlockAction)];
    return self;
}

#pragma mark - runtime objects

- (void)setBlockAction:(BlockAction)blockAction {
    objc_setAssociatedObject(self, @selector(blockAction), blockAction, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (BlockAction)blockAction {
    return objc_getAssociatedObject(self, _cmd);
}

@end



@implementation UIBarButtonItem (modify)
//设置大小
-(void)setFont:(UIFont *)font
{
    [self setTitleTextAttributes:@{NSFontAttributeName :font} forState:UIControlStateNormal];
}
-(UIFont *)font
{
    return nil;
}
//设置颜色
-(void)setColor:(UIColor *)color
{
    [self setTitleTextAttributes:@{NSForegroundColorAttributeName :color} forState:UIControlStateNormal];
}
-(UIColor *)color
{
    return nil;
}
//设添加删除线（提示）
-(void)setDeleteLint:(BOOL)deleteLint
{
    if (deleteLint) {
        [self setTitleTextAttributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle)} forState:UIControlStateNormal];
    }
}

-(BOOL)deleteLint
{
    return YES;
}

//设置背景颜色
-(void)setBackColor:(UIColor *)backColor
{
    [self setTitleTextAttributes:@{NSBackgroundColorAttributeName :backColor} forState:UIControlStateNormal];
}

-(UIColor *)backColor
{
    return nil;
}
-(void)setUnderline:(BOOL)underline
{
    if (underline) {
        [self setTitleTextAttributes:@{NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)} forState:UIControlStateNormal];
    }
}

-(BOOL)underline
{
    return YES;
}


@end

